package jnpf.util;

import jnpf.base.UserInfo;
import jnpf.constant.PermissionConstant;
import jnpf.permission.entity.OrganizeAdminIsTratorEntity;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.service.OrganizeAdminIsTratorService;
import jnpf.permission.service.OrganizeService;
import jnpf.util.context.SpringContext;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分级管理工具类
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021-11-01
 */
public class PermissionAspectUtil {

    private static OrganizeService organizeService = null;
    private static OrganizeAdminIsTratorService organizeAdminIsTratorService;

    static {
        organizeService = SpringContext.getBean(OrganizeService.class);
        organizeAdminIsTratorService = SpringContext.getBean(OrganizeAdminIsTratorService.class);
    }

    /**
     * 判断是否为管理员
     *
     * @param userInfo
     * @return
     */
    public static boolean getIsAdministrator(UserInfo userInfo) {
        //跳过管理员
        if (userInfo.getIsAdministrator()) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否含有操作权限
     *
     * @param organizeId
     * @param userInfo
     * @param methodName
     * @return
     */
    public static boolean containPermission(String organizeId, UserInfo userInfo, String methodName) {
        OrganizeEntity organizeEntity = organizeService.getInfo(organizeId);
        if (organizeEntity != null) {
            // 当前用户的所有分级权限
            List<OrganizeAdminIsTratorEntity> list = organizeAdminIsTratorService.getOrganizeAdminIsTratorEntity(userInfo.getUserId());
            List<OrganizeAdminIsTratorEntity> organizeAdminIsTratorEntities = list.stream().filter(t -> organizeId.equals(t.getOrganizeId())).collect(Collectors.toList());
            // 存在则验证是否有当前组织分级管理 创建权限
            if (PermissionConstant.METHOD_CREATE.equals(methodName)) {
                if (organizeAdminIsTratorEntities.size() > 0 && organizeAdminIsTratorEntities.get(0).getThisLayerAdd() == 1) {
                    return true;
                }
            } else if (PermissionConstant.METHOD_UPDATE.equals(methodName)) {
                // 存在则验证是否有当前组织分级管理 编辑权限
                if (organizeAdminIsTratorEntities.size() > 0 && organizeAdminIsTratorEntities.get(0).getThisLayerEdit() == 1) {
                    return true;
                }
            } else if (PermissionConstant.METHOD_DELETE.equals(methodName)) {
                // 存在则验证是否有当前组织分级管理 编辑权限
                if (organizeAdminIsTratorEntities.size() > 0 && organizeAdminIsTratorEntities.get(0).getThisLayerDelete() == 1) {
                    return true;
                }
            }
            // 查看父级的组织权限是否含有子集权限
            List<OrganizeEntity> organizeList = organizeService.getList();
            if (recursiveParent(organizeList, organizeEntity, list, methodName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断组织新建权限(从父级给的子集新建操作权限)
     *
     * @param organizeList
     * @param organizeEntity
     * @param list
     * @return
     */
    private static boolean recursiveParent(List<OrganizeEntity> organizeList, OrganizeEntity organizeEntity, List<OrganizeAdminIsTratorEntity> list, String methodName) {
        // 得到父级组织
        List<OrganizeEntity> organizeParentList = organizeList.stream().filter(t -> organizeEntity.getParentId().equals(t.getId())).collect(Collectors.toList());
        if (organizeParentList.size() > 0) {
            // 得到父级的权限
            List<OrganizeAdminIsTratorEntity> collect = list.stream().filter(t -> organizeParentList.get(0).getId().equals(t.getOrganizeId())).collect(Collectors.toList());
            // 判断是创建方法还是修改方法
            if (PermissionConstant.METHOD_CREATE.equals(methodName)) {
                if (collect.size() > 0 && collect.get(0).getSubLayerAdd() == 1) {
                    return true;
                }
            } else if (PermissionConstant.METHOD_UPDATE.equals(methodName)) {
                if (collect.size() > 0 && collect.get(0).getSubLayerEdit() == 1) {
                    return true;
                }
            } else if (PermissionConstant.METHOD_DELETE.equals(methodName)) {
                if (collect.size() > 0 && collect.get(0).getSubLayerDelete() == 1) {
                    return true;
                }
            }
            // 当前正在判断的组织已经是顶级则无需递归
            if (!"-1".equals(organizeEntity.getParentId())) {
                return recursiveParent(organizeList, organizeParentList.get(0), list, methodName);
            }
        }
        return false;
    }

}
